package com.zysd.collection.scheduler.system.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Desc <p>跨域解决方案</p>
 *
 * @author liuht
 * @date 2017/8/18
 */
@Configuration
@WebFilter(urlPatterns = "/**")
public class CorsFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        // Access-Control-Allow-Origin: 指定授权访问的域
        //此优先级高于@CrossOrigin配置
        response.addHeader("Access-Control-Allow-Origin", "*");

        // Access-Control-Allow-Methods: 授权请求的方法（GET, POST, PUT, DELETE，OPTIONS等)
        response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH");

        response.addHeader("Access-Control-Allow-Headers", "Content-Type");

        //30 min
        response.addHeader("Access-Control-Max-Age", "1800");

        filterChain.doFilter(request, response);
    }
}
